home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / LIB / CALCMNSD.C < prev    next >
C/C++ Source or Header  |  1996-01-05  |  2KB  |  66 lines

  1. /* ============ */
  2. /* calcmnsd.c    */
  3. /* ============ */
  4. #include <stdio.h>
  5. #include <miscdefs.h>
  6. #include <math.h>
  7.  
  8. /* ------------------- */
  9. /* FUNCTION PROTOTYPES */
  10. /* ------------------- */
  11. # undef F
  12. # if defined(__STDC__) || defined(__PROTO__)
  13. #    define  F( P )  P
  14. # else
  15. #    define  F( P )  ()
  16. # endif
  17.  
  18. /* INDENT OFF */
  19. extern    double    CalcHarm F((int, UINT));
  20.  
  21. # undef F
  22. /* INDENT ON */
  23.  
  24. #
  25. /* ==================================================================== */
  26. /* CalcMeanStdDev - Calculates Coupon-Width Mean & Standard Deviation    */
  27. /* ==================================================================== */
  28. void
  29. CalcMeanStdDev(UINT Width, double *Mean, double *StdDev)
  30. {
  31.     /* --------------------------------------------------------- */
  32.     /* Width is maximum number of unique integers in a data set. */
  33.     /* These equations were taken from D. E. Knuth, "The Art of  */
  34.     /* Computer Programming," Volume 2, Seminumerical Algorithms */
  35.     /* (1981), page 536 (top).                     */
  36.     /* --------------------------------------------------------- */
  37.     *Mean   = Width * (CalcHarm(1, Width) - 1);
  38.     *StdDev = sqrt(SQR((double)Width)*(CalcHarm(2, Width) - 1)
  39.         - *Mean);
  40. }
  41. # if defined(TEST_MNSD)
  42. #include "calcharm.c"
  43. void
  44. main()
  45. {
  46.     while (main)
  47.     {
  48.     UINT    NumSegs, Unique;
  49.     double    Mean, StdDev;
  50.  
  51.     AbortGracefully();
  52.  
  53.     GetUint("Enter Number of Unique Integers in Data Set: ",
  54.         &Unique);
  55.     GetUint("Enter Number of Segments to be Counted: ", &NumSegs);
  56.  
  57.     CalcMeanStdDev(Unique, &Mean, &StdDev);
  58.     printf("%15u  Unique Integers\n%15u  Segments\n",
  59.         Unique, NumSegs);
  60.     printf("%15.f  Mean Number of Variates Required\n",
  61.         ceil(Mean * NumSegs));
  62.     printf("%15.f  Standard Deviation\n", ceil(StdDev * sqrt(NumSegs)));
  63.     }
  64. }
  65. # endif
  66.